package org.example.hanlder.filter;

import org.apache.commons.lang.StringUtils;
import org.example.constant.MessageConstant;
import org.example.dto.req.UserRegisterReqDTO;
import org.example.exception.*;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * 用户之注册过滤器之验证参数是否为空
 */
@Component
public class UserRegisterParamNotNullChainFilter implements UserRegisterChainFilter<UserRegisterReqDTO>{

    private static final Pattern PHONE_PATTERN = Pattern.compile(MessageConstant.PHONE_REGEX);
    @Override
    public void handler(UserRegisterReqDTO reqDTO) {
        if (StringUtils.isEmpty(reqDTO.getUserName())) {
            throw new UserNameNotNullException(MessageConstant.USERNAME_NOT_NULL);
        }

        if (StringUtils.isEmpty(reqDTO.getPassword())) {
            throw new PasswordNotNullException(MessageConstant.PASSWORD_NOT_NULL);
        }

        if (StringUtils.isEmpty(reqDTO.getEmail())) {
            throw new EmailNotNullException(MessageConstant.EMAIL_NOT_NULL);
        }

        if (StringUtils.isEmpty(reqDTO.getPhone())) {
            throw new PhoneNotNullException(MessageConstant.PHONE_NOT_NULL);
        }

        if (StringUtils.isNotEmpty(reqDTO.getPhone()) && !PHONE_PATTERN.matcher(reqDTO.getPhone()).matches()) {
            throw new PhoneFormatException(MessageConstant.PHONE_FORMAT_ERROR);
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
